package com.robot.mvvm.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import cn.wandersnail.ble.*
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.view.CommonDialog
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppFragmentMyBinding
import com.robot.mvvm.vm.main.MyFragmentViewModel
import kotlinx.coroutines.*
import me.goldze.mvvmhabit.BR
import java.util.*

class MyFragment : GLBaseFragment<AppFragmentMyBinding, MyFragmentViewModel>(), EventObserver {

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_my

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initRefresh()

        initEvent()
    }

    private fun initRefresh() {

    }

    private fun initEvent() {

    }

    override fun obtainData() {
        super.obtainData()
    }

    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.uc.logoutEvent.observe(this) {
            showLogoutDialog()
        }
    }

    private fun showLogoutDialog() {
        CommonDialog.Builder(requireContext())
            .setDes("是否确认退出")
            .setDesColor(resources.getColor(R.color.app_333))
            .setPositiveButton("确认", resources.getColor(R.color.app_333)) { type, dialog ->
                dialog.dismiss()
                viewModel.logout()
            }.setNegativeButton(
                "取消",
                resources.getColor(R.color.app_333)
            ) { type, dialog ->
                dialog.dismiss()
            }
            .create().show()
    }
}